from app import app,db,request,jsonify
from app.controller.Naming import Naming
from app.controller.controller import BzNaming
from app.common.common import res_json
from app.controller.NameTest import NameTest
#八字分析测试
@app.route('/api/v1/bz_wx',methods=['GET','POST'])
def bz_wx():
    date_s = request.form.get('date_s') or None
    time_s = request.form.get('time_s') or None
    if date_s == None or time_s == None or not len(date_s) == 10 or len(time_s) > 2 or int(time_s)>24:
        return jsonify({
            'code':201,
            'msg':'参数错误'
        })
    n =  Naming(date_s,time_s)
    bz = n.bazi()['bazi_arr']
    wx = n.wuxing(bz)
    wx_fx = n.wx_fx(bz,wx)
    return jsonify({
        'code':200,
        'data':{
            'bz':bz,
            'wx':wx,
            'describe':wx_fx['describe'],
            'wx_arr':wx_fx['wx_arr'],
        }
    })
#八字起名接口
@app.route('/api/v1/bz_qm',methods=['POST'])
def bz_qm():
    date_s = request.form.get('date_s') or None
    time_s = request.form.get('time_s') or None
    surname = request.form.get('surname') or None
    sex = request.form.get('sex') or None
    page = request.form.get('page') or 1
    if date_s == None or time_s == None or not len(date_s) == 10 or len(time_s) > 2 or int(time_s)>24 or surname == None or sex == None:
        return jsonify({
            'code':201,
            'msg':'参数错误'
        })
    n =  BzNaming(date_s,time_s,surname,sex,page)
    name_result =  n.sun_func()
    return res_json(200,name_result,'success')

@app.route('/api/v1/name_test',methods=['POST'])
def name_test():
    date_s = request.form.get('date_s') or None
    time_s = request.form.get('time_s') or None
    full_name = request.form.get('full_name') or None
    sex = request.form.get('sex') or None
    if date_s == None or time_s == None or not len(date_s) == 10 or len(time_s) > 2 or int(time_s)>24:
        return jsonify({
            'code':201,
            'msg':'参数错误'
        })
    test = NameTest(full_name,sex)
    score = test.count_score()
    n =  Naming(date_s,time_s)
    bz = n.bazi()['bazi_arr']
    wx = n.wuxing(bz)
    wx_fx = n.wx_fx(bz,wx)
    return jsonify({
        'code':200,
        'data':{
            'bz':bz,
            'wx':wx,
            'describe':wx_fx['describe'],
            'wx_arr':wx_fx['wx_arr'],
            'score':score
        }
    })


# #获取五行方法 入参：个人姓名如 马明瑞
# @app.route('/api/v1/abc1234',methods=['POST'])
# def get_menu():
#     name = '马明瑞'
#     name_arr = list(map(str, name))
#     result_arr = []
#     try:
#         for item in name_arr:
#             result_arr.append({
#                 item:{
#                     'menn':ZiDian.query.filter_by(hanzi=item).first().jianjie,
#                     'wx':Wxhanzi.query.filter_by(hanzi=item).first().wx
#                 }
#             })
#     except:
#         pass
#         return jsonify(result_arr)
#     return jsonify(result_arr)











